package com.lx.fphonelocation

import android.Manifest
import android.content.Context
import android.util.Log
import com.github.dfqin.grantor.PermissionListener
import com.github.dfqin.grantor.PermissionsUtil
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding
import io.flutter.plugin.common.MethodChannel
import java.util.Objects

/** FphonelocationPlugin  */
internal class FphonelocationPlugin : FlutterPlugin {
    private val TAG = "FnativelocationPlugin"
    private var methodChannel: MethodChannel? = null
    private var mContext: Context? = null
    override fun onAttachedToEngine(binding: FlutterPluginBinding) {
        mContext = binding.applicationContext
        methodChannel = MethodChannel(binding.binaryMessenger, "com.lx.fnativelocation")
        methodChannel!!.setMethodCallHandler { call, result ->
            val method = call.method
            val arguments = call.arguments
            when (method) {
                "init" -> initFN(arguments)
                "startLocation" -> LocationUtil.getInstance(mContext).startLocation()
                "stopLocation" -> LocationUtil.getInstance(mContext).stopLocation()
            }
        }
    }

    override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
        methodChannel!!.setMethodCallHandler(null)
    }

    private fun initFN(arguments: Any?) {
        if (arguments != null) {
            val argumentsTemp = arguments as Map<String, Any>
            val isSingleO = argumentsTemp["isSingle"]
            var isSingle = false
            if (isSingleO != null) isSingle = isSingleO as Boolean
            val delayMilTimeO: Any = argumentsTemp["delayMilTime"] as Int
            var delayMilTime = 5000
            if (delayMilTimeO != null) delayMilTime = delayMilTimeO as Int
            LocationUtil.getInstance(mContext).setIsSingle(isSingle).setDelayMilTime(delayMilTime)
        }
        LocationUtil.getInstance(mContext).setLocationCallback { latitude, longitude ->
            Log.e(TAG, "LocationUtil-->latitude=$latitude,longitude=$longitude")
            val parms: MutableMap<String, Double> = HashMap()
            parms["latitude"] = latitude
            parms["longitude"] = longitude
            if (methodChannel != null) methodChannel!!.invokeMethod("locationCall", parms)
        }
        checkPermissions()
    }

    private fun checkPermissions() {
        if (PermissionsUtil.hasPermission(mContext!!, *mPermissions)) {
            Log.e(TAG, "9")
            //有访问权限
            LocationUtil.getInstance(mContext).startLocation()
        } else {
            PermissionsUtil.requestPermission(mContext, object : PermissionListener {
                override fun permissionGranted(permissions: Array<String>) {
                    Log.e(TAG, "10")
                    //用户授予了访问权限
                    LocationUtil.getInstance(mContext).startLocation()
                }

                override fun permissionDenied(permissions: Array<String>) {
                    Log.e(TAG, "11")
                    //用户拒绝了访问的申请
                }
            }, *mPermissions)
        }
    }

    companion object {
        //申请的权限
        private val mPermissions = arrayOf(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION //            ,Manifest.permission.READ_PHONE_STATE
            ,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
    }
}